package prolog.fluents;

import prolog.terms.Const;
import prolog.terms.Copier;
import prolog.terms.Prog;
import prolog.terms.Source;
import prolog.terms.Term;

/**
  Merges a List of Sources into a new Source which
  (fairly) iterates over them breadth first.
*/
 class SourceMerger extends prolog.fluents.JavaSource1,prolog.terms.Prog("p") {
    def SourceMerger(Xs:Const,p:Prog){
    super.p;
    this.Q=new Queue(Copier.ConsToVector(Xs));
  }
  
  private var Q:Queue;
  
  def getElement():Term = {
    if(null==Q)
       null;
    while(!Q.isEmpty()) {
      Source current=(Source)Q.deq();
      if(null==current)
        continue;
      Term T=current.getElement();
      if(null==T)
        continue;
      Q.enq(current);
      T;
    }
    null;
  }
}